package yl.hs.bmipfull.controllers.driverApp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.vehiclemanage.TruckInfoEntity;
import yl.hs.bmipfull.service.laboratory.IProductCateService;
import yl.hs.bmipfull.service.laboratory.IProductOverageService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.vehiclemanage.ITruckInfoService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.viewmodels.OptionModel;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 司机端主数据查询控制器
 * @since 2022/5/18 16:27 创建
 */
@RequestMapping("/api/driverApp/driverMain")
@Api(value = "司机端主数据查询Web接口", tags = "司机端app——司机端主数据查询Web接口")
@RestController
public class DriverMainController extends ControllerBase {


    @Autowired
    ITransTaskService transTaskService;
    @Autowired
    ITruckInfoService truckInfoService;
    @Autowired
    IProductShipOrderService productShipOrderService;
    @Autowired
    IProductOverageService Overageservice;
    @Autowired
    IProductCateService cateService;



    @ApiOperation(value = "查询车辆信息派车信息", notes = "根据生产任务单查询派车信息")
    @RequestMapping(value = "/GetTranTask/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult GetTranTaskByTaskSN(@PathVariable("rowNum")int rowNum, @PathVariable("page")int page, @ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            pageQuery=initTruckQuery(pageQuery);
            var truckSNList=pageQuery.getQuery().get("truckSNList");
            if(truckSNList!=null){
                return success("查询成功", transTaskService.queryPageByDriver(pageQuery,rowNum,page));
            }else{
                return success("查询成功", null);
            }
        } catch (Exception e) {
            return error(e);
        }

    }
    @ApiOperation(value = "查询产品类别表", notes = "查询产品类别表")
    @RequestMapping(value = "getCate", method = RequestMethod.GET)
    public WebResult queryList() {
        try {
            List<OptionModel> model = new ArrayList<>();
            var result = cateService.findList();
            result.forEach(t -> {
                model.add(new OptionModel(t.getSn(), t.getCateName(), null, t));
            });
            return success("查询成功", model);
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询产品发货单", notes = "分页查询产品发货单")
    @RequestMapping(value = {"/GetShipOrder/{rowNum}/{page}"}, method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var cateSN=query.getQuery().get("cateSN").toString();
            query=initTruckQuery(query);
            if(query==null){
                return success("ok", null);
            }
            var truckSNList=query.getQuery().get("truckSNList");
            if(truckSNList!=null){
                var result = productShipOrderService.queryPage(query, rowNum, page,cateSN);
                return success("ok", result);
            }else{
                return success("ok", null);
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询产品发货单", notes = "根据产品发货单编号查询产品发货单")
    @RequestMapping(value = "GetShipOrder/{sn}", method = RequestMethod.GET)
    public WebResult findShipOrderBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", productShipOrderService.findBySn(sn));
    }
    @ApiOperation(value = "排队信息看板", notes = "排队信息看板")
    @RequestMapping(value = "/Dispatch/{useType}/{cateSN}", method = RequestMethod.GET)
    public WebResult queryAll(@ApiParam(value = "车辆类型") @PathVariable String useType, @ApiParam(value = "产品类别") @PathVariable String cateSN) {

        return success("查询成功", truckInfoService.queryAll(useType, cateSN));
    }
    @ApiOperation(value = "查询车辆信息", notes = "根据车辆信息编号查询车辆信息")
    @RequestMapping(value = "truckInfo/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", truckInfoService.findBySn(sn));
    }
    public PageQuery initTruckQuery(PageQuery result){
        try {
            var user= HsApp.getCurrent();
            var truckQuery=new PageQuery();
            truckQuery.getQuery().put("driverSN",user.getUserSN());
            List<TruckInfoEntity> truckList= null;
            truckList = truckInfoService.queryList(truckQuery);
            if (truckList.size()>0){
                List<String> truckSNList=new ArrayList<>();
                for (var truck:truckList) {
                    truckSNList.add(truck.getSn());
                }
                result.getQuery().put("truckSNList",truckSNList);
            }else{
                return null;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  result;
    }

}
